import moment from "moment";
import { getFromStorage } from "../../localCache/lilyLocalCache";
moment.locale("zh-cn");

/**
 * Join left and right part of a URL, remove or add "/", to ensure a single "/" in between.
 * @param {string} left - left part of a url
 * @param {string} right - right part of a url
 * @returns
 */
export function joinURL(left, right) {
  while (left[left.length - 1] === "/") {
    left = left.slice(0, left.length - 1);
  }
  while (right[0] === "/") {
    right = right.slice(1);
  }
  return left + "/" + right;
}
/**
 * 检查token是否存在，判断当前登录是否有效
 * @returns bool
 */
export const isAuthenticated = () => {
  // 这里应该有逻辑来检查用户的认证状态，例如检查本地存储或状态管理中的 token
  const token = getFromStorage("access_token");

  return !!token; // 如果有 token，返回 true，否则返回 false
};

/**
 * 格式化日期为 yyyy-mm-dd 格式
 * @param {*} date
 * @returns
 */
export function formattedDate(date) {
  if (date === "") {
    return "";
  } else {
    return moment(date).format("YYYY-MM-DD");
  }
}
